package com.bjpowernode.stusys.controller;

import com.bjpowernode.stusys.entity.Admin;
import com.bjpowernode.stusys.exception.SysException;
import com.bjpowernode.stusys.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author jy
 * @version 1.0
 * @date 2021/12/1 10:24
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/register")
    public String register(Admin admin, Model model){
        /*
            admin相当与绑定了前端的请求数据
            直接将admin交给业务层处理  符合MVC的设计模式
            如果业务逻辑层出现了异常表示出册失败：转发会注册页面
            如果没有出现异常表示注册成功：重定向到登录页面
         */
        try {
            adminService.register(admin);
            return "redirect:/login.jsp";
        }catch (SysException e){
            model.addAttribute("msg",e.getMessage());
            return "register";
        }
    }

    @RequestMapping("/login")
    public String login(Admin admin,Model model){
        try {
            adminService.login(admin);
            return "redirect:/student/page";
        }catch (SysException e){
            model.addAttribute("msg",e.getMessage());
            return "login";
        }
    }
}
